<template>
  <el-dialog :append-to-body="true" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog page" width="1200px" @open="open" @close="close">
    <el-form class="tl-form stakeholderForm" ref="form" :model="model" :rules="rules" label-width="350px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="评分项目（大类）" prop="scoringitem_big_catergory" :rules="rules.validateRequire">
            <el-input v-model="model.scoringitem_big_catergory" :disabled="disableEditSubmit"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="评分项目（小类）" prop="scoringitem_small_catergory" >
            <el-input v-model="model.scoringitem_small_catergory" :disabled="disableEditSubmit"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="评分细则" prop="scoringrubric" :rules="rules.validateRequire">
            <el-input v-model="model.scoringrubric" type="textarea" :rows="3" :disabled="disableEditSubmit"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="细则描述" prop="scoredescription">
            <el-input v-model="model.scoredescription" type="textarea" :rows="3" :disabled="disableEditSubmit"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="满分" prop="fullscore" :rules="rules.validateRequire">
            <el-input v-model="model.fullscore" @change="changescore" oninput="this.value=this.value.match(/\d{0,}\.?\d{0,1}/)" :disabled="disableEditSubmit"></el-input>
            <span class="unitSpan">分</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="目标得分" prop="targetscore" :rules="rules.validateRequire">
            <el-input v-model="model.targetscore" @change="changescore" oninput="this.value=this.value.match(/\d{0,}\.?\d{0,1}/)" :disabled="disableEditSubmit"></el-input>
            <span class="unitSpan">分</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="自评分" prop="selfscore" :rules="rules.validateRequire">
            <el-input v-model="model.selfscore" @change="changescore" oninput="this.value=this.value.match(/\d{0,}\.?\d{0,1}/)" :disabled="disableEditSubmit"></el-input>
            <span class="unitSpan">分</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分差" prop="scoregap" :rules="rules.validateRequire">
            <el-input v-model="model.scoregap" :disabled="true"></el-input>
            <span class="unitSpan">分</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="专家是否指导自评分" prop="isexpertguide" :rules="rules.validateRequire">
            <el-select v-model="model.isexpertguide" filterable clearable :disabled="disableEditSubmit">
              <el-option v-for="item in codeModel.enabledList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item :label="'基于专家意见的差距分析'"  prop="gapanalysis" :rules="rules.validateRequire">
            <el-input v-model="model.gapanalysis" type="textarea" show-word-limit maxlength="1000" :rows="3" :disabled="disableEditSubmit"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="提分计划" prop="scoringplan" :rules="rules.validateRequire">
            <el-input v-model="model.scoringplan" type="textarea" show-word-limit maxlength="1000" :rows="3" :disabled="disableEditSubmit"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12" v-if="times==='1'">
          <el-form-item label="责任人" prop="responsible" :rules="rules.validateRequire">
            <tl-ref-user v-model="model.responsiblecode" :initDisplayData.sync="model.responsible" :singleSelect="true"  dataType="code"></tl-ref-user>
            <!-- <tl-ref-stakeholder v-model="model.responsible"  :initDisplayData.sync="model.responsible" fieldKey="usercode" :externalSelectedRowKeys="model.responsiblecode" title="选择责任人" :externalQueryParam="externalQueryParam" :isSingleSelect="true" :disabled="disableEditSubmit"></tl-ref-stakeholder> -->
          </el-form-item>
        </el-col>
        <el-col :span="12" v-else>
          <el-form-item label="责任人" prop="responsible" :rules="rules.validateRequire">
            <tl-ref-stakeholder v-model="model.responsible" @confirm="addConfirm" :initDisplayData.sync="model.responsible" fieldKey="usercode" :externalSelectedRowKeys="model.responsiblecode" title="选择责任人" :externalQueryParam="externalQueryParam" :isSingleSelect="true" :disabled="disableEditSubmit"></tl-ref-stakeholder>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划完成日期" prop="planenddate" :rules="disablePlanEndDate ? [] : rules.validateRequire">
            <el-date-picker v-model="model.planenddate" type="date" :picker-options="pickerOptions" placeholder="计划完成日期" value-format="yyyy-MM-dd" :disabled="disableEditSubmit || disablePlanEndDate"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
				<el-col :span="12">
					<el-form-item label="实际完成日期" prop="actualenddate" >
						<el-date-picker v-model="model.actualenddate" type="date" placeholder="实际完成日期" value-format="yyyy-MM-dd"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row> -->
      <el-row>
        <el-col :span="24" v-if="model.completion && model.completion !== null && (model.actualenddate && model.actualenddate !== null)">
          <el-form-item label="进度更新" prop="completion">
            <el-input v-model="model.completion" type="textarea" :rows="3" show-word-limit maxlength="1000" :disabled="disableEditSubmit"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12" v-if="model.completionscore && model.completionscore !== null && (model.actualenddate && model.actualenddate !== null)">
          <el-form-item label="完成得分" prop="completionscore">
            <el-input v-model="model.completionscore" :disabled="disableEditSubmit"></el-input>
            <span class="unitSpan">分</span>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="(model.feedbackscore && model.feedbackscore !== null) || fromReview">
          <el-form-item label="专家反馈得分" prop="feedbackscore" :rules="fromReview ? rules.validateRequire : []">
            <el-input v-model="model.feedbackscore" type="number" :disabled="disableEditSubmit"></el-input>
            <span class="unitSpan">分</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24" v-if="(model.losspointandadvice && model.losspointandadvice !== null) || fromReview">
          <el-form-item label="失分点及改进意见" prop="losspointandadvice" :rules="fromReview ? rules.validateRequire : []">
            <el-input v-model="model.losspointandadvice" type="textarea" :rows="3" show-word-limit maxlength="1000" :disabled="disableEditSubmit"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <tl-approval-history ref="history" sourceType="MpmProjectBidScore" :sourceId="model.id" />
    <div style="float:right">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave && !fromReview && !isChange"><svg-icon icon-class="save" />保存</el-button>
      <!-- <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval"><svg-icon icon-class="send" />提交审批</el-button> -->
      <!--  <tl-attachment sourceType="MpmProjectBidScore" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit"></tl-attachment>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory"><svg-icon icon-class="list" />审批历史</el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval"><svg-icon icon-class="undo" />取消审批</el-button> -->
    </div>
    <div v-if="feedbackList && feedbackList.length > 0" class="tl-form-line-title">历史反馈</div>
    <feed-back-his ref="feedbackhisref" :feedbackList="feedbackList"></feed-back-his>
    <div slot="footer" class="dialog-footer"></div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
import { validateNull } from '@tapui/utils/validate'
/** 子组件 */
import TlRefStakeholder from '../mpmProjectStakeholder/refList'
import FeedBackHis from './feedBackHis'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { TlRefStakeholder, FeedBackHis },
  data() {
    return {
      moduleTitle: '评标细则信息表',
      urlPrefix: '/mpm/projectBidScore/',
      url: {
        // 额外的url
        queryMpmInfo: '/mpm/projectBidScore/queryMpmInfo/',
        queryFeedBackIsApproval: '/mpm/projectBidScore/queryFeedBackIsApproval'
      },
      externalQueryParam: {
        qry_projectcode: this.projectinfo.projectcode // 项目编号
      },
      pickerOptions: {
        disabledDate: (time) => {
          if (this.projectinfo.bidscoreflag === 1) {
            return
          } else {
            return time.getTime() > this.biddate.getTime() || time.getTime() < new Date().getTime()
          }
        }
      },
      type: '1',
      times: '1',
      isChange: false,
      feedbackList: []
    }
  },
  props: {
    projectinfo: {},
    biddate: {
      type: Date,
      default: function() {
        return new Date('2030-01-01 00:00:00')
      }
    },
    // 是否来自复盘页面
    fromReview: {
      type: Boolean,
      default: false
    },
    totalfullscore: {
      type: String,
      default: ''
    }
  },
  computed: {
    disablePlanEndDate() {
      return !validateNull(this.model.fullscore) && !validateNull(this.model.selfscore) && this.model.fullscore === this.model.selfscore
    }
  },
  methods: {
    beforeSubmit() {
      this.$set(this.model, 'totalfullscore', this.totalfullscore)
      this.$set(this.model, 'bidscoreflag', this.projectinfo.bidscoreflag)
      // isChange = true时，表示变更
      if (this.isChange) {
        this.$set(this.model, 'isChange', true)
      } else {
        this.$set(this.model, 'isChange', false)
      }
      return true
    },
    afterSubmit() {
      if (this.isChange) {
        this.$emit('queryIsRefresh', this.model.id)
      }
      this.visible = false
    },
    assignDefaultValue() {
      if (this.isAdd) {
        if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
          this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        }
        this.$set(this.model, 'deptcode', this.dept.code)
        this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'projectcode', this.projectinfo.projectcode)
        this.$set(this.model, 'improvescore', '0')
        // this.$set(this.model, 'scoringitem_big_catergory', this.projectinfo.projectname)
        this.$set(this.model, 'type', this.type)
        this.$set(this.model, 'times', this.times)
      }
    },
    queryFeedBackIsApproval() {
      getAction(this.url.queryFeedBackIsApproval, { id: this.model.id }).then((res) => {
          if (res.success) {
            this.feedbackList = res.result.feedbackList
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
    },
    loaded() {
      this.queryFeedBackIsApproval()
    },
    open() {
      this.model = {}
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = '新增' + this.moduleTitle
          this.assignDefaultValue()
        } else if (this.disableEditSubmit === false) {
          this.title = '编辑' + this.moduleTitle
          this.queryData(this.id).then((_) => {
            this.loaded()
          })
        } else {
          this.title = '查看' + this.moduleTitle
          this.queryData(this.id).then((_) => {
            this.loaded()
          })
        }
      })
    },
    changescore() {
      // 满分
      let fullscore = parseFloat(this.model.fullscore ? this.model.fullscore : 0)
      // 自评分
      let selfscore = parseFloat(this.model.selfscore ? this.model.selfscore : 0)
      // 目标得分
      let targetscore = parseFloat(this.model.targetscore ? this.model.targetscore : 0)
      if (fullscore > 100) {
        this.$tap.alert('满分不能大于100！')
        this.$set(this.model, 'fullscore', '100')
        this.$set(this.model, 'scoregap', (100 - selfscore + '').indexOf('.') > 0 ? Number(100 - selfscore).toFixed(1) + '' : Number(100 - selfscore) + '')
        return
      }
      if (fullscore < selfscore) {
        this.$tap.alert('自评分不能大于满分！')
        this.$set(this.model, 'selfscore', '0')
        this.$set(this.model, 'scoregap', fullscore + '')
        return
      }
      if (fullscore < targetscore) {
        this.$tap.alert('目标得分不能大于满分！')
        this.$set(this.model, 'targetscore', '0')
        return
      }
      if (selfscore > targetscore && this.model.targetscore) {
        this.$tap.alert('自评分不能大于目标得分！')
        this.$set(this.model, 'selfscore', '0')
        this.$set(this.model, 'scoregap', fullscore + '')
        return
      }
      if (fullscore >= 0) {
        let a = (fullscore - selfscore + '').indexOf('.') > 0 ? Number(fullscore - selfscore).toFixed(1) : Number(fullscore - selfscore)
        this.$set(this.model, 'scoregap', (fullscore - selfscore + '').indexOf('.') > 0 ? Number(fullscore - selfscore).toFixed(1) + '' : Number(fullscore - selfscore) + '')
      }
    },
    onSelectResponsible(selectedCboUser) {
      const { code, name } = selectedCboUser[0]
      this.model.responsiblecode = code
      this.model.responsible = name
    },
    // 人员选择后赋值
    addConfirm(rows, keys, values) {
      this.$set(this.model, 'responsiblecode', keys)
      this.$set(this.model, 'responsible', values)
    }
  }
}
</script>
<style scoped lang="scss">.stakeholderForm {
  margin-top: 15px;
  margin-left: -220px;
  .el-row {
    margin-bottom: 2px;
  }
  /* 隐藏禁用状态下的提示 */
  .is-disabled ::placeholder {
    color: transparent !important;
  }
}
.tl-ref-user {
  /deep/ .tl-ref-selector {
    .el-button {
      background-color: red !important;
    }
  }
}
</style>
